package com.example.bomb;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class MainActivity extends AppCompatActivity {

    private Button btnReg;
    private Button btnLogin;

    private Button btnInfo;
    private Button btnLogout;
    private Button btnPhone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnLogin = findViewById(R.id.btn_login);
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,LoginActivity.class );
                startActivity(intent);
            }
        });

        btnReg = findViewById(R.id.btn_reg);
        btnReg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,RegActivity.class );
                //intent.setClass(MainActivity.this,RegActivity.class );
                startActivity(intent);
            }
        });

        btnInfo = findViewById(R.id.btn_user_info);
        btnInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(BmobUser.getCurrentUser() == null){
                    Toast.makeText(MainActivity.this,"请您登录", Toast.LENGTH_SHORT).show();
                }
                else{
                    MyUser myUser = BmobUser.getCurrentUser(MyUser.class);

                    final String info = myUser.getUsername() + myUser.getName()+myUser.getAge();
                    BmobQuery<Dog> query = new BmobQuery<>();
                    query.addWhereEqualTo("userid", myUser.getObjectId());
                    query.findObjects(new FindListener<Dog>() {
                        @Override
                        public void done(List<Dog> list, BmobException e) {
                            if(e == null){
                                StringBuffer buffer = new StringBuffer();
                                buffer.append(info);
                                for(Dog dog : list){
                                    buffer.append(dog.getName()).append(dog.getColor()).append(dog.getAge());
                                }
                                Toast.makeText(MainActivity.this,"用户信息" +buffer.toString(), Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }
            }
        });

        btnLogout = findViewById(R.id.btn_log_out);
        btnLogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BmobUser.logOut();
            }
        });


        btnPhone = findViewById(R.id.btn_phone);
        btnPhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,PhoneActivity.class );
                startActivity(intent);
            }
        });
    }
}